package com.kylin.shared.utils;

import com.kylin.shared.enums.*;
import com.kylin.shared.exception.BusinessException;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.Objects;

/**
 * 对象映射工具类
 * @author Damon S.
 * @version v1.0.1
 * @date 2020年01月12日 14:19
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class MappingTools {

    public static long toCurrentEpochSecond() {
        return toEpochSecond(LocalDateTime.now());
    }

    public static long toEpochSecond(LocalDateTime ldt) {
        if (Objects.isNull(ldt)) {
            throw new BusinessException(ResultCodeEnum.DATA_ERROR);
        }
        return ldt.toEpochSecond(OffsetDateTime.now().getOffset());
    }

    public static LocalDateTime toLocalDateTime(long second) {
        if (second <= 0) {
            return LocalDateTime.MIN;
        }
        return LocalDateTime.ofEpochSecond(second, 0, OffsetDateTime.now().getOffset());
    }

    public static ApprovalState toApprovalState(int code) {
        return ApprovalState.parse(code);
    }

    public static YesNoEnum toYesNoEnum(int code) {
        return YesNoEnum.parse(code);
    }

    public static RunningState toRunningState(int code) {
        return RunningState.parse(code);
    }

    public static OrderState toOrderState(int code) {
        return OrderState.parse(code);
    }

    public static CampaignState toCampaignState(int code) {
        return CampaignState.parse(code);
    }

    public static SwitchState toSwitchState(int code) {
        return SwitchState.parse(code);
    }

    public static GrouponState toGrouponState(int code) {
        return GrouponState.parse(code);
    }

    public static GenderEnum toGenderEnum(int code) {
        return GenderEnum.parse(code);
    }

    public static MarketingType toMarketingType(int code) {
        return MarketingType.parse(code);
    }

}
